package com.xb.system.Filter;

import com.google.common.hash.BloomFilter;
import com.xb.system.config.BusinessEmailBloomFilterConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author lzy
 * @create 2024/11/8 16:02
 */
@Component
@Slf4j
public class GlobalBloomFilter {
    private BloomFilter<String> businessEmailBloomFilter;

    @Autowired
    private BusinessEmailBloomFilterConfig businessEmailBloomFilterConfig;

    @PostConstruct
    public void init() {
        // 初始化布隆过滤器
        businessEmailBloomFilter = businessEmailBloomFilterConfig.createBloomFilter();

        // 如果需要从数据库加载初始数据（可选），加载一次就好
//        loadEmailsFromDatabase();
    }

//    private void loadEmailsFromDatabase() {
//        // 仅在程序启动时加载一次数据
//        List<String> emails = loadEmailsFromDataSource(); // 从数据库或者其他数据源加载邮箱
//        for (String email : emails) {
//            businessEmailBloomFilter.put(email);  // 加载到布隆过滤器
//        }
//        log.info("Loaded {} business emails into Bloom filter", emails.size());
//    }
//
//    private List<String> loadEmailsFromDataSource() {
//        // 这里可以是从数据库、缓存等地方加载去重数据
//        // 例如：return usersInfoService.getAllBusinessEmails();
//        return List.of("example1@example.com", "example2@example.com"); // 示例数据
//    }

    public BloomFilter<String> getBusinessEmailBloomFilter() {
        return businessEmailBloomFilter;
    }
}
